package com.example.soa_exp2.Controller;

import com.example.soa_exp2.Config.CommonResponse;
import com.example.soa_exp2.Domain.Borrow;
import com.example.soa_exp2.Domain.Goods;
import com.example.soa_exp2.Domain.Student;
import com.example.soa_exp2.Service.BorrowService;
import com.example.soa_exp2.Service.GoodsService;
import com.example.soa_exp2.Service.StudentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.List;

@RestController
@Tag(name = "借用器材模块")
@RequestMapping("/borrowings")
public class BorrowController {

    @Resource
    public BorrowService borrowService;

    @Resource
    public StudentService studentService;

    @Resource
    public GoodsService goodsService;
    @Autowired
    public Student stu;

    @Autowired
    public Goods goods;

    @Autowired
    public Borrow borrow;

    @Operation(summary = "增加借用记录")
    @PostMapping("/stu/{stu_num}")
    public CommonResponse AddBorrowGoodsRec(@PathVariable String stu_num, @RequestParam String goods_id, @RequestParam Long bor_num,@RequestParam String operator_account){

        try {
            Goods goods = goodsService.getGoodsById(goods_id);
            if (goods == null) {
                return CommonResponse.BackForError("找不到商品：" + goods_id);
            }

            Long goods_stock = goods.getStock();
            if (bor_num > goods_stock) {
                return CommonResponse.BackForError("器材" + goods.getGoods_name() + " 库存不足");
            } else {
                Student stu = studentService.getStudentByNum(stu_num);
                if (stu == null) {
                    return CommonResponse.BackForError("找不到学生：" + stu_num);
                }

                borrow.setGoods_name(goods.getGoods_name());
                borrow.setGoods_id(goods.getGoods_id());
                borrow.setStu_name(stu.getName());
                borrow.setStu_tel(stu.getTel());
                borrow.setStu_id(stu_num);
                borrow.setBor_num(bor_num);
                borrow.setBor_time(new Date(System.currentTimeMillis()));
                borrow.setOperator_account(operator_account);
                borrow.setStatus(false);
                borrow.setBor_rec_id(0);

                //差一个对库存的操作
                int borrowResult = borrowService.addBorrow(borrow);


                if (borrowResult == 1) {
                    // 在这里进行事务提交
                    return CommonResponse.BackForSuccess("新增成功","http://127.0.0.1:8081/borrowings/stu/"+stu_num);
                } else {
                    return CommonResponse.BackForError("新增失败");
                }
            }
        } catch (Exception e) {
            // 异常处理，返回适当的错误响应
            e.printStackTrace();  // 在实际应用中，你可能需要更详细的日志记录
            return CommonResponse.BackForError("发生异常：" + e.getMessage());
        }



    }


    @Operation(summary = "通过bor_id查询借用记录")
    @GetMapping("/{bor_request_id}")
    public CommonResponse<Borrow> getBorRequestById(@PathVariable int bor_request_id) {
        borrow = borrowService.getBorrowById(bor_request_id);
        if (borrow != null) {
            return CommonResponse.BackForSuccess("查询成功", borrow,"http://127.0.0.1:8081/borrowings/"+bor_request_id);
        } else {
            return CommonResponse.BackForError("没有相关查询记录");
        }
    }

    @Operation(summary = "通过goods_id查询借用记录")
    @GetMapping("/goods_id")
    public CommonResponse<List> getBorRequestByGoodsId(@RequestParam String goods_id){
        List<Borrow> borrowList = borrowService.getBorrowByGoodsId(goods_id);
        if (borrow != null) {
            if (borrowList.size()>0)
                return CommonResponse.BackForSuccess("查询成功", borrowList,"http://127.0.0.1:8081/borrowings/goods/"+goods_id);
            else
                return CommonResponse.BackForSuccess("查询成功，没有相关记录", borrowList,"http://127.0.0.1:8081/borrowings/goods/"+goods_id);

        } else {
            return CommonResponse.BackForError("没有相关查询记录");
        }
    }

    @Operation(summary = "通过goods_name查询借用记录")
    @GetMapping("/goods_name")
    public CommonResponse<List> getBorRequestByGoodsName(@RequestParam String goods_name){
        List<Borrow> borrowList = borrowService.getBorrowByGoodsName(goods_name);
        if (borrowList != null) {
            if(borrowList.size()>0)
                return CommonResponse.BackForSuccess("查询成功", borrowList,"http://127.0.0.1:8081/borrowings/goods/"+goods_name);
            else
                return CommonResponse.BackForSuccess("查询成功,没有相关记录", borrowList,"http://127.0.0.1:8081/borrowings/goods/"+goods_name);


        } else {
            return CommonResponse.BackForError("没有相关查询记录");
        }
    }

    @Operation(summary = "通过学生id查询借用记录")
    @GetMapping("/stu_num")
    public CommonResponse<List> getBorRequestByStuNum(@RequestParam String stu_num){
        List<Borrow> borrowList = borrowService.getBorrowByStuId(stu_num);
        if (borrowList != null) {
            if(borrowList.size()>0)
                return CommonResponse.BackForSuccess("查询成功", borrowList,"http://127.0.0.1:8081/borrowings/stu_num/"+stu_num);
            else
                return CommonResponse.BackForSuccess("查询成功,没有相关记录", borrowList,"http://127.0.0.1:8081/borrowings/stu_num/"+stu_num);


        } else {
            return CommonResponse.BackForError("没有相关查询记录");
        }
    }

    @Operation(summary = "通过学生姓名查询借用记录")
    @GetMapping("/stu_name")
    public CommonResponse<List> getBorRequestByStuName(@RequestParam String stu_name){
        List<Borrow> borrowList = borrowService.getBorrowByStuName(stu_name);
        if (borrowList != null) {
            if(borrowList.size()>0)
                return CommonResponse.BackForSuccess("查询成功", borrowList,"http://127.0.0.1:8081/borrowings/stu_name/"+stu_name);
            else
                return CommonResponse.BackForSuccess("查询成功,没有相关记录", borrowList,"http://127.0.0.1:8081/borrowings/stu_name/"+stu_name);


        } else {
            return CommonResponse.BackForError("没有相关查询记录");
        }
    }

    @Operation(summary = "通过起始时间查询借用记录")
    @GetMapping("/datePeriod")
    public CommonResponse<List> getBorRequestByDatePeriod(@RequestParam String date_begin,@RequestParam String date_finish){
        List<Borrow> borrowList = borrowService.getBorrowByTime(date_begin,date_finish);
        if (borrowList != null) {
            if(borrowList.size()>0)
                return CommonResponse.BackForSuccess("查询成功", borrowList,"http://127.0.0.1:8081/borrowings/datePeriod/"+date_begin+"-"+date_finish);
            else
                return CommonResponse.BackForSuccess("查询成功,没有相关记录", borrowList,"http://127.0.0.1:8081/borrowings/datePeriod/"+date_begin+"-"+date_finish);


        } else {
            return CommonResponse.BackForError("没有相关查询记录");
        }
    }

    @Operation(summary = "通过确定时间查询借用记录")
    @GetMapping("/date")
    public CommonResponse<List> getBorRequestByDate(@RequestParam String date){
        List<Borrow> borrowList = borrowService.getBorrowByTime(date);
        if (borrowList != null) {
            if(borrowList.size()>0)
                return CommonResponse.BackForSuccess("查询成功", borrowList,"http://127.0.0.1:8081/borrowings/datePeriod"+date);
            else
                return CommonResponse.BackForSuccess("查询成功,没有相关记录", borrowList,"http://127.0.0.1:8081/borrowings/datePeriod"+date);


        } else {
            return CommonResponse.BackForError("没有相关查询记录");
        }
    }

}
